<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 题目1(选做)：const arr=['name','age','hhh','hhh','age','hhh','hhh','hhh'] 元素各出现多少次
        // 要求输出一个对象如下：{age: 2,hhh: 5,name: 1}
        const arr = ['name', 'age', 'hhh', 'hhh', 'age', 'hhh', 'hhh', 'hhh']
        // let newArr = []
        function getCount(arr) {
            let obj = {}
            for (let i = 0; i < arr.length; i++) {
                // const element = array[i];
                if (obj[arr[i]]) {
                    obj[arr[i]]++
                } else {
                    obj[arr[i]] = 1
                }
            }
            return obj
        }
        console.log(getCount(arr));
        // console.log(obj);

        // 题目2(必做)：现在两个数组，判断在a中的元素，不在b中 const a = [1,2,3,5] const b=[1,3,5,6] 将在a不在b中的筛选出来
        // 要求：输出一个数组：[2]
        const a = [1, 2, 3, 5]
        const b = [1, 3, 5, 6]
        const includesArr = a.filter((item, index) => !b.includes(item))
        console.log(includesArr);
        // 题目3(必做):数组去重(可以用多种方法,至少写一种)
        // 要求：输出一个数组：[1,2,3,9]
        
        // set
        const arr2 = [1, 2, 3, 2, 9]
        const setArr = new Set(arr2)
        const fromArr = Array.from(setArr)
        console.log(fromArr);

        // foreach
        function getSetArr(arr) {
            const newArr = []
            arr.forEach((item, index) => {
                if (newArr.indexOf(item) === -1) {
                    newArr.push(item)
                }
            })
            return newArr
        }
        console.log(getSetArr(arr2));



    </script>
</body>

</html>